package de.grosjohann.android.feuerwehr;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import de.grosjohann.android.feuerwehr.adapter.FahrzeugListAdapter;
import de.grosjohann.android.feuerwehr.domain.Fahrzeug;
import de.grosjohann.android.feuerwehr.notification.Notify;
import de.grosjohann.android.feuerwehr.panel.Panel;
import de.grosjohann.android.feuerwehr.service.FMSDisplayService;
import de.grosjohann.android.feuerwehr.service.IntentServiceFacade;
import de.grosjohann.android.feuerwehr.util.JSON2ObjectUtil;

public class FMSActivity
  extends Activity
  implements OnItemClickListener, IUpdateUIListener {

  public static final String FMS_AUTHORIZED = "feuerwehr.FMS_AUTHORIZED";

  private static final String NAME = "FMSActivity";

  private boolean authorized = false;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.fms);

    Bundle extras = getIntent().getExtras();
    if (extras != null && extras.containsKey(FMS_AUTHORIZED)) {
      authorized = extras.getBoolean(FMS_AUTHORIZED);
    }

    ListView lv = (ListView) findViewById(R.id.lvFMS);
    BaseAdapter baseAdapter = new FahrzeugListAdapter(this, R.layout.fahrzeug);
    lv.setAdapter(baseAdapter);
    lv.setDividerHeight(1);

    lv.setOnItemClickListener(this);

    // Setting for FMSService
    FMSDisplayService.updateUIListener = this;
  }

  @Override
  protected void onResume() {
    super.onResume();
    Notify.getInstance().clear(this);

    IntentServiceFacade.startOnce(this, FMSDisplayService.class);
  }

  /*
   * Update of FMSData.
   */
  private void updateFMSList(String newData) {
    try {
      ListView lv = (ListView) findViewById(R.id.lvFMS);
      ((FahrzeugListAdapter) lv.getAdapter()).addAll(JSON2ObjectUtil.getFahrzeugList(newData));

      TextView tvNoStatus = (TextView) findViewById(R.id.tvNoStatus);
      tvNoStatus.setVisibility(View.GONE);
    } catch (JSONException ex) {
      Log.e(NAME, ex.getMessage(), ex);

      Panel panel = new Panel(getApplicationContext(), this);
      panel.showError(R.string.errReceiveFMSData);
    }
  }

  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Fahrzeug fahrzeug = ((FahrzeugListAdapter) parent.getAdapter()).getItem(position);

    if (authorized) {
      Intent intent = new Intent(this, StatusActivity.class);
      intent.putExtra(StatusActivity.FMS_OBJECT, fahrzeug);
      startActivity(intent);
    } else {

      if (fahrzeug.hasLocation()) {
        Intent mapsIntent = new Intent(this, MapsActivity.class);
        mapsIntent.putExtra(MapsActivity.OBJECT, fahrzeug);
        mapsIntent.putExtra(MapsActivity.LOCATION, fahrzeug.getLocation());
        startActivity(mapsIntent);
      } else {
        Panel panel = new Panel(getApplicationContext(), this);
        panel.showWarning(R.string.lblNoLocation);
      }
    }
  }

  @Override
  public void updateUI(final String newData) {
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        updateFMSList(newData);
      }
    });
  }

  @Override
  public void showInfo(final String info) {
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        Panel panel = new Panel(getApplicationContext(), FMSActivity.this);
        panel.showInfo(info);
      }
    });
  }

  @Override
  public void showWarning(final String warning) {
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        Panel panel = new Panel(getApplicationContext(), FMSActivity.this);
        panel.showWarning(warning);
      }
    });
  }

  @Override
  public void showError(final String error) {
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        Panel panel = new Panel(getApplicationContext(), FMSActivity.this);
        panel.showError(error);
      }
    });
  }

}
